package com.edais.actions;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.domain.advert.UserQuestionnaire;
import com.edais.service.advert.UserQuestionnaireService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.IsObjNullUtil;
import com.edais.util.JsonValidator;
import com.edais.util.Message;

@Controller
@RequestMapping("/question")
public class QuestionnaireAction {
	
	private static final Log logger = LogFactory.getLog(QuestionnaireAction.class);
	
	@Autowired
	private UserQuestionnaireService questionService;
	
	 /**
	  * 问卷提交    
	  * @param request
	  * @param response
	  */
    @RequestMapping(value = "/submit")
    public void submitQuestionnaire(HttpServletRequest request, HttpServletResponse response){
        String result = "";
        String logInfo="问卷提交JavaService";
        try{
            logger.info(logInfo+"开始");
            Map<String, String> paramsMap = CommonUtil.decryptParamters(request);// 解密请求参数
            
            if(StringUtils.isBlank(paramsMap.get("userId"))){
            	logger.info(logInfo +",userId:"+paramsMap.get("userId")+ "校验参数失败");
				  result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
		                    Consts.CHECK_CODE, "",Message.CnDescriptionExpression.PARAMS_IS_NULL);
				  CommonUtil.responseJson(result, response);
				  return;
            }

            Long userId = Long.parseLong(paramsMap.get("userId"));
            String mobile = paramsMap.get("mobile");
            String answers = paramsMap.get("answers");
            String url = paramsMap.get("url");//问卷url
            
            logInfo=logInfo+"userId:"+userId+",mobile:"+mobile;
            if (IsObjNullUtil.isEmpty(mobile, userId, answers, url)) {
				logger.info(logInfo +",url:"+url+",answers:"+answers+ "校验参数失败");
				  result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
		                    Consts.CHECK_CODE, "",Message.CnDescriptionExpression.PARAMS_IS_NULL);
				  CommonUtil.responseJson(result, response);
				  return;
			}
            if(!JsonValidator.valid(answers.trim())){
            	logger.info(logInfo +",url:"+url+",answers:"+answers+ "校验参数失败answers不是json形式");
				  result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
		                    Consts.CHECK_CODE, "","答案不是json形式");
				  CommonUtil.responseJson(result, response);
				  return;
            }
            
            UserQuestionnaire  questionnaire=new UserQuestionnaire(userId, mobile.trim(), url.trim(), answers.trim());
            
            
            questionService.insertQuestionAnswers(questionnaire, logInfo);
            
            
            result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                    Consts.SUCCESS_CODE, "", Consts.SUCCESS_CN);

        }catch (Exception e){
            logger.error(logInfo+"失败",e);
            result =  CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                    Consts.ERROR_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
        }finally {
            logger.info(logInfo+"返回,result="+result);
            // 处理返回结果
            CommonUtil.responseJson(result, response);
        }
    }

}
